package org.batatasframework.support.charts.searchable;

import java.util.ArrayList;
import java.util.List;

import org.batatasframework.support.charts.ResultArray;
import org.batatasframework.support.searchable.ResultHeadInfo;
import org.batatasframework.support.searchable.SearchableInputStream;
import org.batatasframework.support.searchable.writer.AbstractResultWriter;

public class BaiduChartsResultWriter extends AbstractResultWriter implements ResultArray{

	public BaiduChartsResultWriter(SearchableInputStream inputStream) throws Exception {
		super(inputStream);
	}
	private ResultHeadInfo[][] heads;
	private List<String> labels = new ArrayList<>();
	private transient List<Object> line;
	private List<Object[]> data = new ArrayList<>();
	

	@Override
	protected void renderHead(ResultHeadInfo[][] heads) {
		this.heads = heads;
	}

	@Override
	public void append(Object value) throws Exception {
		line.add(value);
	}
	
	@Override
	public void newLine(Object value) throws Exception {
		if(null != line){
			data.add(line.toArray());
		}
		
		line = new ArrayList<>();
		
		labels.add(value.toString());
	}

	@Override
	public void group(Object value, int size) {
		//不应该进来这里
	}
	
	@Override
	public void newLine() throws Exception { 
		//不应该进来这里
	}

	@Override
	public String[] getLabels() {
		return labels.toArray(new String[labels.size()]);
	}

	@Override
	public String[] getAxis() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object[][] getData() {
		if(null != line){
			data.add(line.toArray());
		}
		
		return data.toArray(new Object[data.size()][]);
	}
}
